% align the total allocations to the total emissions by creating one artifitial firm taking the remaining
% allocation 
function[out_data,supply_yearly]=artfac(datafac) 
    period=unique(datafac.period);
    nperiod=length(period);
    R=zeros(nperiod,1);
    E=zeros(nperiod,1);
    artfac=categorical(zeros(nperiod,1));
    artfac_alloc=zeros(nperiod,1);
    artfac_emi=zeros(nperiod,1);
    supply=zeros(nperiod,1);
    for i=1:nperiod
        E(i)=sum(datafac.emi(datafac.period==period(i)));
        R(i)=sum(datafac.alloc(datafac.period==period(i)));
        artfac_alloc(i)=max(0,E(i)-R(i));
        artfac_emi(i)=max(0,R(i)-E(i));
        supply(i)=max(E(i),R(i));
    end
    supply_yearly=table(period,supply);
    data_artfac=table(artfac,period,artfac_alloc,artfac_emi);
    data_artfac.Properties.VariableNames{'artfac'} = 'fac';
    data_artfac.Properties.VariableNames{'artfac_alloc'} = 'alloc';
    data_artfac.Properties.VariableNames{'artfac_emi'} = 'emi';
    out_data=[datafac;data_artfac];
end
